﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Windows.Forms;

namespace UniRlv.Utility
{
    /// <summary>
    /// 如果配置项不存在，AppGetInt是0，AppGetBoolean是false，AppGetString是null，AppGetDate是Datetime.MinValue
    /// </summary>
    public class ConfigurationHelper
    {
        public static string AppGetString(string sec)
        {
            return ConfigurationManager.AppSettings[sec];
        }

        public static int AppGetInt(string sec)
        {
            return Convert.ToInt32(ConfigurationManager.AppSettings[sec]);
        }

        public static double AppGetDouble(string sec)
        {
            return Convert.ToDouble(ConfigurationManager.AppSettings[sec]);
        }

        public static DateTime AppGetDate(string sec)
        {
            return Convert.ToDateTime(ConfigurationManager.AppSettings[sec]);
        }

        public static bool AppGetBoolean(string sec)
        {
            return Convert.ToBoolean(ConfigurationManager.AppSettings[sec]);
        }

        public static void AppSet(string sec, string val)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            
            if(!config.HasFile)
            {
                Console.Out.WriteLine("\n配置文件缺失！！\n\n");
                return;
            }

            if (config.AppSettings.Settings[sec]==null)
            {
                // 添加
                config.AppSettings.Settings.Add(sec, val);
            }
            else
            {
                config.AppSettings.Settings[sec].Value = val;
            }

            config.Save(ConfigurationSaveMode.Modified);
            
            ConfigurationManager.RefreshSection("appSettings");//刷新，否则程序读取的还是之前的值（可能已装入内存）
        }

        public static bool HasSetting(string sec)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

            if (!config.HasFile)
            {
                Console.Out.WriteLine("\n配置文件缺失！！\n\n");
                return false;
            }

            return config.AppSettings.Settings[sec] != null;
        }

        public static bool HasConnString(string sec)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

            if (!config.HasFile)
            {
                Console.Out.WriteLine("\n配置文件缺失！！\n\n");
                return false;
            }

            return config.ConnectionStrings.ConnectionStrings[sec] != null;
        }

        public static void AddConnStringSet(string sec, string connString, string provider)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

            if (!config.HasFile)
            {
                Console.Out.WriteLine("\n配置文件缺失！！\n\n");
                return;
            }

            if (config.ConnectionStrings.ConnectionStrings[sec] == null)
            {
                // 添加
                config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(sec, connString, provider));
            }
            else
            {
                return;
            }

            config.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection("connectionStrings");//刷新，否则程序读取的还是之前的值（可能已装入内存）
        }
    }
}
